<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace api\wxapp\controller;
use cmf\controller\RestBaseController;
use api\wxapp\model\SchoolCourseModel;
use api\wxapp\model\OrderModel;

class CourseController extends RestBaseController
{    
    protected $postModel;
    public function __construct(SchoolCourseModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    public function index()
    {        
        $params = $this->request->param();        
        $data = $this->postModel->paramsFilter($params)            
            ->with(['schoolClass','schoolSubject','schoolTeacher'])          
            ->where(['delete_time'=>0,'status'=>1])
            ->order('list_order ASC')  
            ->select();            
        $this->success('请求成功!', $data);
    }

    public function recommendedList()
    {                     
        $data = $this->postModel->recommendedList();            
        $this->success('请求成功!', $data);
    }

    public function newList()
    {                     
        $data = $this->postModel->newList();            
        $this->success('请求成功!', $data);
    }

    public function info()
    {        
        $params = $this->request->param();        
        $data = $this->postModel->paramsFilter($params)            
            ->with(['schoolClass','schoolSubject','schoolTeacher'])
            ->find(); 
        $timestamp = time();
        $orderModel = new OrderModel();
        $seats = $orderModel->field('seat')->where("course_id={$params['id']} and ({$timestamp} - create_time <= 900 or  pay_status = 1)")->select();
        $arr=array();
        foreach($seats as $item){
            array_push($arr,$item['seat']);
        }
        for($i=1;$i<=$data['num'];$i++){
            if(in_array($i,$arr)){
                $a_seat[$i] = 'true';
            }else{
                $a_seat[$i] = '';
            }
        }
        $data['seats'] = $a_seat;
        $this->success('请求成功!', $data);
    }
}
